package kamikaze.service;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kamikaze.bean.Admin;
import kamikaze.utils.Database;
import kamikaze.utils.Database.Template;
import kamikaze.utils.StringUtil;

public class SetupServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		Database.get().execute(new Template<Object>() {

			@Override
			public Object query(EntityManager em) throws Exception {

				Number count = (Number) em.createQuery("select count(a) from Admin a ").getSingleResult();

				if (count.intValue() != 0) {
					return null;
				}

				Admin admin = new Admin();

				admin.setLoginId("admin");
				admin.setPassword(StringUtil.encrypt("4klykjhs04"));

				em.persist(admin);

				return null;
			}
		});

	}
}
